package hermesV3.protocols;

import hermesV3.agent.AbstractProtocol;
import hermesV3.agent.Interaction;

/**
 * This Protocol manages interactions in the TupleSpace.
 * 
 * @author Ambruoso Luigi
 * @author Addeo Vincenzo
 */
public class TupleSpaceProtocol extends AbstractProtocol {
	/**
	 * 
	 */
	private static final long serialVersionUID = 5276146227452407412L;

	public static final String INTERACTIONREADALL = "ReadAll";
	public static final String INTERACTIONREAD = "Read";

	public static final String INTERACTIONREMOVEALL = "RemoveAll";
	public static final String INTERACTIONREMOVE = "Remove";

	public static final String INTERACTIONOUTPUTRESP = "OutputResp";

	public static final String INTERACTIONWRITEALL = "WriteAll";
	public static final String INTERACTIONWRITE = "Write";
	public static final String INTERACTIONWRITERESP = "WriteResp";

	public static final String INTERACTIONERROR = "Error";

	/**
	 * A {@link TupleSpaceProtocol}.
	 */
	public TupleSpaceProtocol() {
		super("TupleSpaceProtocol");

		String[] parameterNameReadAll = new String[1];
		parameterNameReadAll[0] = "ReadAll";
		Class<?>[] parameterTypeReadAll = new Class[1];
		parameterTypeReadAll[0] = Object.class;
		Interaction readAll = new Interaction(INTERACTIONREADALL,
				INTERACTIONREADALL, INTERACTIONREADALL, parameterNameReadAll,
				parameterTypeReadAll);
		addInteraction(readAll);

		String[] parameterNameOutputResp = new String[1];
		parameterNameOutputResp[0] = "OutputResp";
		Class<?>[] parameterTypeOutputResp = new Class[1];
		parameterTypeOutputResp[0] = Object.class;
		Interaction outputResp = new Interaction(INTERACTIONOUTPUTRESP,
				INTERACTIONOUTPUTRESP, INTERACTIONOUTPUTRESP,
				parameterNameOutputResp, parameterTypeOutputResp);
		addInteraction(outputResp);

		String[] parameterNameRead = new String[1];
		parameterNameRead[0] = "Read";
		Class<?>[] parameterTypeRead = new Class[1];
		parameterTypeRead[0] = Object.class;
		Interaction read = new Interaction(INTERACTIONREAD, INTERACTIONREAD,
				INTERACTIONREAD, parameterNameRead, parameterTypeRead);
		addInteraction(read);

		String[] parameterNameRemoveAll = new String[1];
		parameterNameRemoveAll[0] = "RemoveAll";
		Class<?>[] parameterTypeRemoveAll = new Class[1];
		parameterTypeRemoveAll[0] = Object.class;
		Interaction removeAll = new Interaction(INTERACTIONREMOVEALL,
				INTERACTIONREMOVEALL, INTERACTIONREMOVEALL,
				parameterNameRemoveAll, parameterTypeRemoveAll);
		addInteraction(removeAll);

		String[] parameterNameRemove = new String[1];
		parameterNameRemove[0] = "Remove";
		Class<?>[] parameterTypeRemove = new Class[1];
		parameterTypeRemove[0] = Object.class;
		Interaction remove = new Interaction(INTERACTIONREMOVE,
				INTERACTIONREMOVE, INTERACTIONREMOVE, parameterNameRemove,
				parameterTypeRemove);
		addInteraction(remove);

		String[] parameterNameWrite = new String[1];
		parameterNameWrite[0] = "Write";
		Class<?>[] parameterTypeWrite = new Class[1];
		parameterTypeWrite[0] = Object.class;
		Interaction write = new Interaction(INTERACTIONWRITE, INTERACTIONWRITE,
				INTERACTIONWRITE, parameterNameWrite, parameterTypeWrite);
		addInteraction(write);

		String[] parameterNameWriteAll = new String[1];
		parameterNameWriteAll[0] = "WriteAll";
		Class<?>[] parameterTypeWriteAll = new Class[1];
		parameterTypeWriteAll[0] = Object.class;
		Interaction writeAll = new Interaction(INTERACTIONWRITEALL,
				INTERACTIONWRITEALL, INTERACTIONWRITEALL,
				parameterNameWriteAll, parameterTypeWriteAll);
		addInteraction(writeAll);

		String[] parameterNameWriteResp = new String[1];
		parameterNameWriteResp[0] = "WriteResp";
		Class<?>[] parameterTypeWriteResp = new Class[1];
		parameterTypeWriteResp[0] = Object.class;
		Interaction writeResp = new Interaction(INTERACTIONWRITERESP,
				INTERACTIONWRITERESP, INTERACTIONWRITERESP,
				parameterNameWriteResp, parameterTypeWriteResp);
		addInteraction(writeResp);

		addResponseLink(readAll, outputResp);
		addResponseLink(read, outputResp);
		addResponseLink(removeAll, outputResp);
		addResponseLink(remove, outputResp);
		addResponseLink(write, writeResp);
		addResponseLink(writeAll, writeResp);

		String[] parameterNameError = new String[1];
		parameterNameError[0] = "Error";
		Class<?>[] parameterTypeError = new Class[1];
		parameterTypeError[0] = String.class;
		Interaction error = new Interaction(INTERACTIONERROR, INTERACTIONERROR,
				INTERACTIONERROR, parameterNameError, parameterTypeError);
		addInteraction(error);

		addErrorLink(readAll, error);
		addErrorLink(read, error);
		addErrorLink(removeAll, error);
		addErrorLink(remove, error);
		addErrorLink(outputResp, error);
		addErrorLink(write, error);
		addErrorLink(writeAll, error);
		addErrorLink(writeResp, error);

	}
}
